package org.prs.persistence;

import com.prs.util.PrsException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.prs.model.WorkerComments;
import org.prs.persistence.connector.MySQLDatabaseConnector;

public class WorkerCommentsDatabasePersistence {

    private Connection conn;
    private ResultSet result;

    public boolean insertWorkerComments(WorkerComments workerComments) {
        boolean inserted = true;
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement insertWorkerComments = null;
        try {
            insertWorkerComments = conn.prepareStatement("insert into workercomment set "
                    + " dateofcomment=STR_TO_DATE(?,'%d/%m/%Y'), details=?, childid=?, workerid=?");

            insertWorkerComments.setString(1, workerComments.getDateOfComments());
            insertWorkerComments.setString(2, workerComments.getDetails());
            insertWorkerComments.setInt(3, workerComments.getChildId());
            insertWorkerComments.setInt(4, workerComments.getWorkerId());
            insertWorkerComments.executeUpdate();
        } catch (SQLException e) {
            inserted = false;
            throw new PrsException(e);
        }
        return inserted;
    }

    public WorkerComments getWorkerComments(int workerCommentId) {
        WorkerComments workerComment = new WorkerComments();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectWorkerCommentById = null;
        try {
            selectWorkerCommentById = conn.prepareStatement("select * from workercomment where workercommentid=?");
            selectWorkerCommentById.setInt(1, workerCommentId);
            while (result.next()) {
                workerComment = getWorkerComments(result);
            }
        } catch (SQLException e) {

            throw new PrsException(e);
        }
        return workerComment;
    }

    public List<WorkerComments> getChildWorkerComments(int childId) {
        List<WorkerComments> workerComments = new ArrayList<WorkerComments>();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectWorkerCommentById = null;
        try {
            selectWorkerCommentById = conn.prepareStatement("select * from workercomment where childid=?");
            selectWorkerCommentById.setInt(1, childId);
            while (result.next()) {
                workerComments.add(getWorkerComments(result));
            }
        } catch (SQLException e) {
            throw new PrsException(e);
        }
        return workerComments;
    }

    private WorkerComments getWorkerComments(ResultSet result) {
        WorkerComments workerComment = new WorkerComments();
        try {
            workerComment.setChildId(result.getInt("childid"));
            workerComment.setDateOfComments(result.getString("dateofcomment"));
            workerComment.setDetails(result.getString("details"));
            workerComment.setWorkerId(result.getInt("workerid"));
            workerComment.setWorkerCommentsId(result.getInt("workercommentid"));
        } catch (SQLException e) {

            throw new PrsException(e);
        }
        return workerComment;
    }
}
